package com.sjz.videoRecorder;

import java.io.File;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;

public class VideoRecorderActivity extends Activity     {
	   private SurfaceView surfaceView;
	    private RelativeLayout layout;   //找到相对布局
	    private Button recordbutton;
	    private Button stopbutton;
	    private MediaRecorder mediaRecorder;//媒体刻录类
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);//无标题
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏 
        setContentView(R.layout.main);
        
        surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        /*下面设置Surface不维护自己的缓冲区，而是等待屏幕的渲染引擎将内容推送到用户面前*/
        surfaceView.getHolder().setFixedSize(176, 144);
        //设置尺寸   分辨率
        surfaceView.getHolder().setKeepScreenOn(true);
        //设置常开  不锁屏
        
        layout = (RelativeLayout) this.findViewById(R.id.layout);
        recordbutton = (Button) this.findViewById(R.id.recordbutton);
        stopbutton = (Button) this.findViewById(R.id.stopbutton);
    }
    

     
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){//当用户触摸屏幕的时候  
			layout.setVisibility(ViewGroup.VISIBLE);//按钮可见
		}
		return super.onTouchEvent(event);
	}
	
    
    public void record(View v){
    	switch (v.getId()) {
		case R.id.recordbutton://刻录按钮
			try{
				//创建文件位置
			    File videoFile = 
		            new File(Environment.getExternalStorageDirectory(), 
		            		System.currentTimeMillis()+ ".3gp");
				
				mediaRecorder = new MediaRecorder();
				mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//声音采集麦克风
				mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//视频来自相机
				mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置输出格式
				mediaRecorder.setVideoSize(320, 240);//尺寸
				mediaRecorder.setVideoFrameRate(5);//设置速率
				mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置声音编码
				mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//设置视频编码
				mediaRecorder.setOutputFile(videoFile.getAbsolutePath());//设置文件输出位置
				mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());//设置视频的预览画面
				mediaRecorder.prepare();//进行缓冲
				mediaRecorder.start();//开始刻录
			}catch (Exception e) {
				e.printStackTrace();
			}
			recordbutton.setEnabled(false);//刻录按钮不可用
			stopbutton.setEnabled(true);//停止按钮可用
			break;

		case R.id.stopbutton: //停止按钮
			if(mediaRecorder!=null){
				mediaRecorder.stop();
				mediaRecorder.release();
				mediaRecorder = null;
			}
			recordbutton.setEnabled(true);
			stopbutton.setEnabled(false);
			break;
		}
    }
}